{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "04194.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/04194.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "K207vVBMK8Tw",
        "colab_type": "text"
      },
      "source": [
        "## 5.1 二维卷积层"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "llub0PVILhxO",
        "colab_type": "text"
      },
      "source": [
        "### 5.1.1 二维互相关运算"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GjetXCBXLp4e",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch\n",
        "from torch import nn \n",
        "\n",
        "def corr2d(X, K):\n",
        "    h, w = K.shape \n",
        "    Y = torch.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))\n",
        "    for i in range(Y.shape[0]):\n",
        "        for j in range(Y.shape[1]):\n",
        "            Y[i, j] = (X[i: i + h, j: j + w] * K).sum()\n",
        "    return Y"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "e_meBis1MVWt",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "cbc61abf-d10a-491c-9f87-ad3bfd635726"
      },
      "source": [
        "X = torch.tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]])\n",
        "K = torch.tensor([[0, 1], [2, 3]])\n",
        "corr2d(X, K)"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[19., 25.],\n",
              "        [37., 43.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZC9_34GFMhPk",
        "colab_type": "text"
      },
      "source": [
        "### 5.1.2 二维卷积层"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lVuCvNYnM4Wb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class Conv2D(nn.Module):\n",
        "    def __init__(self, kernel_size):\n",
        "        super(Conv2D, self).__init__()\n",
        "        self.weight = nn.Parameter(torch.randn(kernel_size))\n",
        "        self.bias = nn.Parameter(torch.randn(1))\n",
        "\n",
        "    def forward(self, x):\n",
        "        return corr2d(x, self.weight) + self.bias"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "x0t2g602NRyl",
        "colab_type": "text"
      },
      "source": [
        "### 5.1.3 图像中物体边缘检测"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i9iBwZuHOpmr",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        },
        "outputId": "4894b613-691e-4fb2-dbe4-d653de6430ed"
      },
      "source": [
        "X = torch.ones(6, 8)\n",
        "X[:, 2:6] = 0 \n",
        "X"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 0., 0., 0., 0., 1., 1.],\n",
              "        [1., 1., 0., 0., 0., 0., 1., 1.],\n",
              "        [1., 1., 0., 0., 0., 0., 1., 1.],\n",
              "        [1., 1., 0., 0., 0., 0., 1., 1.],\n",
              "        [1., 1., 0., 0., 0., 0., 1., 1.],\n",
              "        [1., 1., 0., 0., 0., 0., 1., 1.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "E8dbsP4MOvNR",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "K = torch.tensor([[1, -1]])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HX3k5NrTO3lR",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        },
        "outputId": "fb426833-4c14-4d03-a014-78b72e690afe"
      },
      "source": [
        "Y = corr2d(X, K)\n",
        "Y"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.,  1.,  0.,  0.,  0., -1.,  0.],\n",
              "        [ 0.,  1.,  0.,  0.,  0., -1.,  0.],\n",
              "        [ 0.,  1.,  0.,  0.,  0., -1.,  0.],\n",
              "        [ 0.,  1.,  0.,  0.,  0., -1.,  0.],\n",
              "        [ 0.,  1.,  0.,  0.,  0., -1.,  0.],\n",
              "        [ 0.,  1.,  0.,  0.,  0., -1.,  0.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "t1zjfaRcO6co",
        "colab_type": "text"
      },
      "source": [
        "### 5.1.4 通过数据学习核数组"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gnP6a6hgPKJW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "ea6c0c98-b744-4ea9-937f-29e104637616"
      },
      "source": [
        "conv2d = Conv2D(kernel_size=(1, 2))\n",
        "\n",
        "step = 20 \n",
        "lr = 0.01 \n",
        "for i in range(step):\n",
        "    Y_hat = conv2d(X)\n",
        "    l = ((Y_hat - Y) ** 2).sum()\n",
        "    l.backward()\n",
        "\n",
        "    conv2d.weight.data -= lr * conv2d.weight.grad \n",
        "    conv2d.bias.data -= lr * conv2d.bias.grad \n",
        "\n",
        "    conv2d.weight.grad.fill_(0)\n",
        "    conv2d.bias.grad.fill_(0)\n",
        "    if (i + 1) % 5 == 0:\n",
        "        print('Step %d, loss %.3f' % (i + 1, l.item()))"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Step 5, loss 4.475\n",
            "Step 10, loss 1.128\n",
            "Step 15, loss 0.301\n",
            "Step 20, loss 0.082\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zc8F1SSdQOMR",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "2cfdb497-0f7f-4987-b1ee-f4b2f64c2e02"
      },
      "source": [
        "conv2d.weight.data"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.0680, -1.0769]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FuU8C3WDQdrT",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "59b7d25c-d782-4458-ad9a-380ba5b5b586"
      },
      "source": [
        "conv2d.bias.data"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([0.0050])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    }
  ]
}